package com.ldc.security.filter;

import com.ldc.common.utils.ConstantValue;
import com.ldc.common.utils.StringUtils;
import com.ldc.model.SysMenu;
import com.ldc.model.SysRole;
import com.ldc.model.SysRoleMenu;
import com.ldc.service.service.MenuService;
import com.ldc.service.service.RoleMenuService;
import com.ldc.service.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 控制url
 *
 * @auther liudc
 * @since 2023/7/13
 */
@Slf4j
@Component
public class UrlFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    @Autowired
    MenuService menuService;
    @Autowired
    RoleMenuService roleMenuService;
    @Autowired
    RoleService roleService;

    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        // 获取当前url
        final FilterInvocation filterInvocation = (FilterInvocation) object;
        final String requestUrl = filterInvocation.getRequestUrl();
        log.info("请求 url = {}", requestUrl);
        // 查询全部菜单
        List<SysMenu> permissionBeanList = menuService.findListBy(null);
        for (SysMenu item : permissionBeanList) {
            String current_url = item.getUrl();
            log.info("菜单 url = {}  可用 = {}", current_url, item.isEnable());
            if (!item.isEnable()) {
                // 不可用
                return SecurityConfig.createList(ConstantValue.ROLE_LOGIN);
            }
            // 便利菜单
            if (StringUtils.equals(requestUrl, current_url)) {
                // 获取url对应的权限
                List<SysRoleMenu> rolePermissionBeanList = roleMenuService.findListByMenuId(item.getId());
                final String[] roleArray = filterRoleArray(rolePermissionBeanList);
                return SecurityConfig.createList(roleArray);
            }
        }
        // 返回默认
        return SecurityConfig.createList(ConstantValue.ROLE_LOGIN);
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return FilterInvocation.class.isAssignableFrom(clazz);
    }

    /**
     * 筛选权限角色
     *
     * @param list 角色权限映射表
     * @return 角色数组
     */
    String[] filterRoleArray(List<SysRoleMenu> list) {
        List<String> roleList = new ArrayList<>();
        List<Long> roleIdList = list.stream().map(SysRoleMenu::getRoleId)
                .collect(Collectors.toList());
        for (long id : roleIdList) {
            SysRole roleBean = roleService.findByRoleId(id);
            if (roleBean == null) {
                continue;
            }
            roleList.add(roleBean.getRole());
        }
        return roleList.toArray(new String[0]);

    }
}
